# 下载华为云上的笔记，按照年放到不同文件，并且按创建时间从小到大排序
import json
import os
import time
from pathlib import Path
import requests
from datetime import datetime
from bs4 import BeautifulSoup
import html
from PIL.SpiderImagePlugin import isInt

# cookie 和 userId改写为你自己会话中的，浏览器F12后，network中查找
cookie = "cplang=zh-cn; 23c31e7e989bad953c704201a18a88c"
userId = "24008xxxxxxxxxxx0"

#
url_list_query = "https://cloud.huawei.com/notepad/simplenote/query"
url_todo_detail = "https://cloud.huawei.com/notepad/task/query"
url_dairy_detail = "https://cloud.huawei.com/notepad/note/query"

# 如果cookie、userId填写正确还有问题，可以尝试 csrftoken 改写为你的会话中的 csrftoken
csrftoken = "5dd98fc00e1b5f462fb06c6ee068c585979646535213a01a"

def get_base_header():
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/json;charset=UTF-8",
        "cookie": cookie,
        "Content-Type": "application/json",
        "csrftoken": csrftoken,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
        "userId": userId
    }
    return headers

def dairy_json_to_str(item):
    result2 = []
    result2.append("创建时间: " + item["created"])
    result2.append("修改时间: " + item["modified"])
    title = item["title"]
    result2.append(title)
    context = item["content"]
    if "Text|" + title != context:
        result2.append(context)
    return result2

def get_trace_id():
    tmp = str(time.time_ns())
    trace_id = f"03131_02_{tmp[0:10]}_{tmp[10:10+8]}"
    return trace_id


def query_list():
    url = url_list_query
    trace_id = get_trace_id()
    # 要发送的 JSON 数据
    payload = {
        "index":0,
        "status":0,
        "guids":"",
        "traceId":trace_id}

    # 请求头部，带常见鉴权信息
    headers = get_base_header()

    # cookie 信息
    cookies = {
        "userId": userId,
    }

    try:
        # 发送 POST 请求，带超时设置（单位：秒）
        response = requests.post(
            url,
            json=payload,  # 自动转换为 JSON 格式
            headers=headers,
            cookies=cookies,
            timeout=120  # 超时 3s
        )

        # 检查状态码
        response.raise_for_status()

        # 打印返回结果
        print("list状态码: ", response.status_code)
        res = json.loads(response.text)
        return res
    except requests.exceptions.Timeout:
        print("请求超时！")
    except requests.exceptions.RequestException as e:
        print("请求失败:", e)
    return None

def query_context(note_id, node_type):
    if not hasattr(query_context, "num"):
        query_context.num = 0
    query_context.num += 1
    url = url_dairy_detail
    trace_id = get_trace_id()
    # 要发送的 JSON 数据
    payload = {
        "ctagNoteInfo": "",
        "startCursor": "73220",
        "guid": note_id,
        "kind": node_type,
        "traceId": trace_id
    }

    # 请求头部，带常见鉴权信息
    headers = get_base_header()

    # cookie 信息
    cookies = {
        "userId": userId,
    }

    try:
        # 发送 POST 请求，带超时设置（单位：秒）
        response = requests.post(
            url,
            json=payload,  # 自动转换为 JSON 格式
            headers=headers,
            cookies=cookies,
            timeout=3  # 超时 3s
        )

        # 检查状态码
        response.raise_for_status()

        # 打印返回结果
        print("查新日记详情 状态码: ", response.status_code, "  请求次数: ", query_context.num)
        res_data_str = json.loads(response.text).get("rspInfo").get("data")
        if res_data_str == None:
            print("没有查询到数据，检查cookie、csrftoken 是否过期")
            return None
        res_data = json.loads(res_data_str)
        content = res_data.get("content", "")
        # 1. 解析 HTML
        soup = BeautifulSoup(content.get("html_content"), "html.parser")
        # 2. 获取文本（br 标签会自动变成换行）
        raw_text = soup.get_text(separator="\n")
        # 3. 解码 HTML 实体
        decoded_text = html.unescape(raw_text)
        return decoded_text
    except requests.exceptions.Timeout:
        print("请求超时！")
    except requests.exceptions.RequestException as e:
        print("请求失败:", e)
    except Exception as e:
        print("查询context失败，异常信息: ", e)
    return None

def timestamp_to_str(ts):
    """毫秒时间戳转 yyyy/MM/dd hh:mm:ss"""
    ts_int = 0
    if not ts:
        return ""
    elif isInt(ts):
        ts_int = ts
    elif ts.isdigit():
        ts_int = int(ts)
    dt = datetime.fromtimestamp(ts_int / 1000)
    return dt.strftime("%Y/%m/%d %H:%M:%S")

def download_huawei_dairy(output_dir):
    data = query_list()
    note_list = data.get("rspInfo", {}).get("noteList", [])
    res_map = {}

    i = 0
    for r in note_list:
        dairy_data = json.loads(r.get("data", ""))
        create_time = int(dairy_data.get("created", ""))
        read_time_str = timestamp_to_str(dairy_data.get("created"))
        guid = dairy_data.get("guid", "")
        content = None
        while content is None:
            content = query_context(guid, r.get("kind"))
            if content is None:
                print("查询日记详情失败，稍等5s重试...")
                time.sleep(5)
        item = {
            "created": read_time_str,
            "modified": timestamp_to_str(dairy_data.get("modified", "")),
            "title": dairy_data.get("title", ""),
            "content": content,
            "created_time": create_time
        }

        if not read_time_str:
            continue
        year = read_time_str.split("/")[0]
        if year not in res_map:
            res_map[year] = []
        res_map[year].append(item)
        i += 1
        if i >= 5:
            break


    for year in res_map:
        arr = res_map[year]
        arr.sort(key=lambda x: x["created_time"])
        save_path = os.path.join(output_dir, str(year) + ".txt")
        Path(save_path).parent.mkdir(parents=True, exist_ok=True)
        with open(save_path, "w", encoding="utf-8") as f:
            for item in arr:
                iarr = dairy_json_to_str(item)
                iarr.append("\n")
                f.write("\n".join(iarr))
        print("日志保存完成: ", save_path)

if __name__ == "__main__":
    download_huawei_dairy(
        "C:/huawei_download/huawei_dairy/",
    )
